package edu.ptu.acl.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import edu.ptu.acl.common.RequestHolder;
import edu.ptu.acl.utils.SnowIdWorker;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMPObjectHandler implements MetaObjectHandler {
    //插入时自动给类属性赋值
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "creator", String.class, RequestHolder.getCurrentUser().getName());
        this.strictInsertFill(metaObject, "updator", String.class, RequestHolder.getCurrentUser().getName());
        this.strictInsertFill(metaObject, "version", Integer.class, 1);
        this.strictInsertFill(metaObject, "id", Long.class, SnowIdWorker.getSnowId());


    }

    //更新时自动给类属性赋值
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updator", String.class, RequestHolder.getCurrentUser().getName());
    }
}
